using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class SubscriptionButton : MonoBehaviour
{
	public UILabel timer;

	public UILabel infoText;

	public UILabel noConnection;

	public UILabel takeReward;

	public GameObject holder;

	public BoxCollider button;

	public FlyingDrone flyingDrone;

	private bool canTakeReward;

	private bool firstStart = true;

	private bool _inited;

	private IEnumerator timerCoroutine;

	private HashSet<char> rusEndings = new HashSet<char> { '0', '5', '6', '7', '8', '9' };

	private void Awake()
	{
		InAppSubscriptionsController.OnStartSubscription += StartSubscriptionEventHandler;
		InAppSubscriptionsController.OnEndSubscription += EndSubscriptionEventHandler;
		InAppSubscriptionsController.OnStartTimer += StartTimerEventHandler;
		InAppSubscriptionsController.OnJSONLoaded += JSONLoadedEventHandler;
		SwitchLanguage.onChangeLanguage += ShowSubscriptionsTimer;
		FlyingDrone.OnDroneArrived = (Action)Delegate.Combine(FlyingDrone.OnDroneArrived, new Action(StartAndShowTimer));
		if (InAppSubscriptionsController.instance.jsonReaded)
		{
			Init();
		}
		holder.SetActive(false);
	}

	private void OnDestroy()
	{
		InAppSubscriptionsController.OnStartSubscription -= StartSubscriptionEventHandler;
		InAppSubscriptionsController.OnEndSubscription -= EndSubscriptionEventHandler;
		InAppSubscriptionsController.OnStartTimer -= StartTimerEventHandler;
		InAppSubscriptionsController.OnJSONLoaded -= JSONLoadedEventHandler;
		SwitchLanguage.onChangeLanguage -= ShowSubscriptionsTimer;
		FlyingDrone.OnDroneArrived = (Action)Delegate.Remove(FlyingDrone.OnDroneArrived, new Action(StartAndShowTimer));
		if (timerCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(timerCoroutine);
		}
	}

	private void OnClick()
	{
		if (canTakeReward)
		{
			bool flag = InAppSubscriptionsController.instance.TryTakeReward();
			canTakeReward = !flag;
			if (flag)
			{
				settings.ShowLabelWithAnimation(InAppSubscriptionsController.instance.PromoParameters.cycleReward, flyingDrone.rewardCase.transform.position, false);
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("Offer", "1");
				FlurryWrapper.LogEvent(FlurryWrapper.EV_SPECIAL_OFFER_SALES, dictionary);
			}
		}
	}

	private void StartSubscriptionEventHandler()
	{
	}

	private void EndSubscriptionEventHandler()
	{
		SwitchHolder(false);
		flyingDrone.FlyAway();
	}

	private void StartTimerEventHandler()
	{
		SwitchHolder(false);
		if (!flyingDrone.gameObject.activeSelf)
		{
			flyingDrone.gameObject.SetActive(true);
		}
		if (firstStart)
		{
			firstStart = false;
			if (InAppSubscriptionsController.instance.GetCycleRemaininTime() > 0)
			{
				flyingDrone.SetNeedCase(false);
			}
			else
			{
				flyingDrone.SetNeedCase(true);
			}
		}
		else
		{
			flyingDrone.FlyAwayAndReturn(InAppSubscriptionsController.instance.GetCycleRemaininTime() <= 0);
		}
	}

	private void StartAndShowTimer()
	{
		StartTimer();
		ShowSubscriptionsTimer();
		SwitchHolder(true);
	}

	private void JSONLoadedEventHandler()
	{
		Init();
	}

	private void Init()
	{
		if (!_inited)
		{
			if (InAppSubscriptionsController.instance.GetSubscriptionState())
			{
				StartTimerEventHandler();
			}
			else
			{
				EndSubscriptionEventHandler();
			}
			_inited = true;
		}
	}

	private void SwitchHolder(bool val)
	{
		holder.SetActive(val);
		button.enabled = val;
	}

	private void StartTimer()
	{
		if (timerCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(timerCoroutine);
		}
		timerCoroutine = TimerIEnumerator(InAppSubscriptionsController.instance.GetCycleRemaininTime());
		CoroutineRunner.Instance.StartCoroutine(timerCoroutine);
	}

	private string GetLocalizationForRemainingDays(int days)
	{
		string text = "day_remained1";
		string result = "day_remained2";
		string text2 = "day_remained3";
		string text3 = days.ToString();
		char c = text3[text3.Length - 1];
		if (LocalizationManager.IsEnglish())
		{
			return (days != 1) ? text : text2;
		}
		if (LocalizationManager.IsRussian())
		{
			if (rusEndings.Contains(c) || days == 11 || days == 12 || days == 13 || days == 14)
			{
				return text;
			}
			if (c == '2' || c == '3' || c == '4')
			{
				return result;
			}
			return text2;
		}
		return text;
	}

	private void ShowSubscriptionsTimer()
	{
		int subscriptionRemaininDays = InAppSubscriptionsController.instance.GetSubscriptionRemaininDays();
		infoText.text = string.Format(ScriptLocalization.Get("Game_Common/" + GetLocalizationForRemainingDays(subscriptionRemaininDays)), subscriptionRemaininDays);
	}

	private IEnumerator TimerIEnumerator(int secondsToOpen)
	{
		int currentTimer = secondsToOpen;
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			noConnection.gameObject.SetActive(true);
			timer.gameObject.SetActive(false);
			infoText.gameObject.SetActive(false);
			takeReward.gameObject.SetActive(false);
		}
		else
		{
			noConnection.gameObject.SetActive(false);
			timer.gameObject.SetActive(true);
			infoText.gameObject.SetActive(true);
			takeReward.gameObject.SetActive(false);
		}
		int noInternet = 0;
		while (!JSONManager.Instance.ServerTimeLoad())
		{
			noInternet++;
			yield return null;
		}
		if (noInternet > 0)
		{
			Init();
			yield break;
		}
		while (currentTimer > 0)
		{
			TimeSpan t = TimeSpan.FromSeconds(currentTimer);
			string time = ((t.Hours > 0) ? ((t.Hours <= 9) ? string.Format("{0:D1}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds)) : ((t.Minutes > 0) ? ((t.Minutes <= 9) ? string.Format("{0:D1}:{1:D2}", t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds)) : ((t.Seconds <= 9) ? string.Format("{0:D1}", t.Seconds) : string.Format("{0:D2}", t.Seconds))));
			timer.text = time;
			yield return new WaitForSeconds(1f);
			currentTimer--;
		}
		button.enabled = false;
		timer.gameObject.SetActive(false);
		if (flyingDrone.HasRewardCase)
		{
			takeReward.gameObject.SetActive(true);
			canTakeReward = true;
		}
		if (!flyingDrone.HasRewardCase)
		{
			flyingDrone.FlyAwayAndReturn(true);
		}
		yield return new WaitForSeconds(1f);
	}
}
